/**
 * 
 */
package cn.biplam.front.template.base;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlImage;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Visibility_Mode;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.template.Template;

/**
 * 占满按钮大小的小型图片按钮(用于标题区16*16)
 * @author 	WangSong
 * @create 	2009-4-10 下午05:28:56
 * @version 1.0
 */
public class ButtonImageSmall implements Template{
	
	private HtmlDiv button;
	private String id;
	private String imageUrl;
	private String imageUrlShow;
	private int width=16;
	private int height=16;
	
	/**
	 * 构造方法
	 * @param ID		页面ID
	 * @param imageUrl	按钮图片
	 */
	public ButtonImageSmall(String id,String imageUrl){
		this(id,imageUrl,null);
	}

	/**
	 * 构造方法
	 * @param ID		页面ID
	 * @param imageUrl	按钮图片
	 * @param imageUrlShow	鼠标移动到时按钮图片
	 */
	public ButtonImageSmall(String id,String imageUrl,String imageUrlShow){
		this.id=id;
		this.imageUrl=imageUrl;
		this.imageUrlShow=imageUrlShow;
		this.button=new HtmlDiv(this.id,"sib");
	}
	
	/**
	 * 绑定左键点击事件
	 * @param sScript
	 * @throws ErrorMessage
	 */
	public void bind(String sScript) throws ErrorMessage{
		this.button.bind(Window_Event.OnClick, sScript);
	}

	/**
	 * 返回设置好的Html控件
	 * @return	HtmlWindow
	 * @throws ErrorMessage 
	 */
	public HtmlWindow get() throws ErrorMessage{
		HtmlDiv image=this._getInnerDiv(this.id+"u",this.imageUrl);
		this.button.put(image);
		this.button.setStyleSize(width+2, height+2);
		
		if(this.imageUrlShow!=null){
			HtmlDiv imageShow=this._getInnerDiv(this.id+"s",this.imageUrlShow);
			this.button.put(imageShow);
			imageShow.setVisibility(Visibility_Mode.Hidden);
			
			button.bind(Window_Event.OnmouseOver, "sis(this);");
			button.bind(Window_Event.OnMouseOut, "siu(this);");
		}
		
		return this.button;
	}
	
	/**
	 * 创建一个用于图片切换的内置Div
	 * @param check 是否是已选择图片,true 是 ,false不是
	 * @return HtmlDiv
	 */
	private HtmlDiv _getInnerDiv(String divId,String url) throws ErrorMessage{
		HtmlDiv mDiv = new HtmlDiv(divId,"si");
		mDiv.setStyleSize(width, height);
		HtmlImage mImage=new HtmlImage(url);
		mImage.setHeight(String.valueOf(this.height));
		mImage.setWidth(String.valueOf(this.width));
		mDiv.put(mImage);
		return mDiv;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	
	/**
	 * 返回本页面控件的占位像素
	 * @return
	 */
	public int getLength(){
		return this.width+2;
	}


}
